﻿using UnityEngine;

namespace MYSTARFORCE
{
    /// <summary>
    /// 循环滚动背景脚本
    /// </summary>
    public class ScrollBg : MonoBehaviour
    {
        /// <summary>
        /// 滚动速度
        /// </summary>
        [SerializeField]
        private float m_ScrollSpeed = -0.01f;

        /// <summary>
        /// 两个背景图的物体
        /// </summary>
        [SerializeField]
        private Transform[] transforms = null;

        [SerializeField]
        private BoxCollider m_VisibleBoundary = null;

        [SerializeField]
        private BoxCollider m_PlayerMoveBoundary = null;

        [SerializeField]
        private BoxCollider m_EnemySpawnBoundary = null;

        /// <summary>
        /// 可显示区域
        /// </summary>

        public BoxCollider VisibleBoundary
        {
            get
            {
                return m_VisibleBoundary;
            }
        }

        /// <summary>
        /// 我方战机生成区域
        /// </summary>
        public BoxCollider PlayerMoveBoundary
        {
            get
            {
                return m_PlayerMoveBoundary;
            }
        }

        /// <summary>
        /// 敌方战机生成区域
        /// </summary>
        public BoxCollider EnemySpawnBoundary
        {
            get
            {
                return m_EnemySpawnBoundary;
            }
        }

        // Update is called once per frame
        private void Update()
        {
            // 循环设置背景图的坐标
            foreach (var item in transforms)
            {
                SetPosition(item);
            }
        }

        /// <summary>
        /// 设置坐标
        /// </summary>
        /// <param name="transform"></param>
        private void SetPosition(Transform transform)
        {
            Vector3 newPosition = transform.position + Vector3.forward * m_ScrollSpeed;
            // 如果坐标小于等于-30，就把它挪到上面去，加60
            if(newPosition.z <= -30)
            {
                newPosition.z += 60;
            }
            transform.position = newPosition;
        }

    }
}